<!DOCTYPE html>


<html lang="zh-CN">
  

    <head>
      <meta charset="utf-8" />
       
      <meta name="keywords" content="ES6、JS" />
       
      <meta name="description" content="ES6、JS" />
      
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>模拟组件库开发流程 |  君书</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="君书" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-模拟组件库开发流程"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  模拟组件库开发流程
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2022/03/21/%E6%A8%A1%E6%8B%9F%E7%BB%84%E4%BB%B6%E5%BA%93%E5%BC%80%E5%8F%91%E6%B5%81%E7%A8%8B/" class="article-date">
  <time datetime="2022-03-21T08:57:53.000Z" itemprop="datePublished">2022-03-21</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">10 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="VUEUI"><a href="#VUEUI" class="headerlink" title="VUEUI"></a>VUEUI</h1><p>模拟组件库开发（Vue2 框架）</p>
<span id="more"></span>

<h2 id="一-准备工作"><a href="#一-准备工作" class="headerlink" title="一. 准备工作"></a>一. 准备工作</h2><p>使用第三方开源工具 <code>storybook</code> 进行组件开发，方便组件的独立运行和测试。</p>
<h3 id="安装插件"><a href="#安装插件" class="headerlink" title="安装插件"></a>安装插件</h3>  <figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ npx -p @storybook/cli sb init --<span class="built_in">type</span> vue</span><br><span class="line">$ yarn add vue</span><br><span class="line">$ yarn add vue-loader vue-template-compiler --dev</span><br></pre></td></tr></table></figure>
<p>推荐使用 <code>yarn</code> 安装插件，因为后期会使用 <code>yarn workspace</code> 进行多包仓库管理（monorep）。</p>
<h2 id="二-开发组件库"><a href="#二-开发组件库" class="headerlink" title="二. 开发组件库"></a>二. 开发组件库</h2><h3 id="1-组件主要分三类："><a href="#1-组件主要分三类：" class="headerlink" title="1. 组件主要分三类："></a>1. 组件主要分三类：</h3><ol>
<li><p>第三方组件</p>
<p> 以 vue 框架为基础，常用的第三方组件有 ElementUI</p>
</li>
<li><p>基础组件</p>
</li>
<li><p>业务组件</p>
</li>
</ol>
<h3 id="2-组件库开发项目的组织方式主要有两种："><a href="#2-组件库开发项目的组织方式主要有两种：" class="headerlink" title="2. 组件库开发项目的组织方式主要有两种："></a>2. 组件库开发项目的组织方式主要有两种：</h3><ol>
<li><code>multirepo</code>（多个仓库开发，一个项目/组件 对应一个仓库）</li>
<li><code>monorepo</code>（一个仓库中存在多个项目/组件）</li>
</ol>
<p>本次模拟组件库的开发项目组织方式选用 <code>monorepo</code> 形式。配合 <code>yarn workspace</code> 使用，可以避免开发时不同包依赖同一插件时，该插件的重复安装。</p>
<p>采用 <code>monorepo</code> 时，仓库的文件夹结构如下：</p>
<ul>
<li>.storybook</li>
<li>packages<ul>
<li>button<ul>
<li>src<ul>
<li>xxx.vue (单文件组件)</li>
</ul>
</li>
<li>stories<ul>
<li>index.stories.js (stories)</li>
</ul>
</li>
<li><code>__test__</code></li>
<li>index.js (提供 install 方法，方便通过 Vue.use(Com) 注册组件)</li>
<li>package.json</li>
<li>README.md</li>
</ul>
</li>
</ul>
</li>
<li>package.json</li>
</ul>
<p>根目录下的 <code>package.json</code> 文件中工作区的具体配置如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;private&quot;</span>: <span class="literal">true</span>,  <span class="comment">// 提交 github 或 npm 时，根目录下的文件不提交</span></span><br><span class="line">  <span class="attr">&quot;workspaces&quot;</span>: [<span class="string">&quot;packages/*&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在工作区安装插件的相关命令：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 给工作区根目录安装开发依赖</span></span><br><span class="line">$ yarn add jest -D -W </span><br><span class="line"><span class="comment">// 给指定的工作区安装依赖</span></span><br><span class="line">$ yarn workspace lg-button add lodash@4</span><br><span class="line"><span class="comment">// 给所有工作区安装依赖</span></span><br><span class="line">$ yarn install</span><br><span class="line"><span class="comment">// 运行所有工作区内的同一指令</span></span><br><span class="line">$ yarn workspaces run del</span><br></pre></td></tr></table></figure>
<h3 id="3-组件的开发"><a href="#3-组件的开发" class="headerlink" title="3. 组件的开发"></a>3. 组件的开发</h3><p>组件库中所有的组件都存放在 packages 文件夹下，下面以 <code>Button</code> 为例，介绍下组件的开发流程：（开发内容存放在 <code>packages/button</code> 文件夹下）</p>
<ol>
<li>单文件组件 <code>src/button.vue</code></li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;button :class&#x3D;&quot;cls&quot;&gt;</span><br><span class="line">    &lt;slot &#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;button&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &#39;XfjButton&#39;,</span><br><span class="line">  props: [&#39;type&#39;],</span><br><span class="line">  computed: &#123;</span><br><span class="line">    cls() &#123;</span><br><span class="line">      return [&#39;xfjbtn&#39;, &#123; </span><br><span class="line">        &#39;primary-xfjbtn&#39;: this.type &#x3D;&#x3D;&#x3D; &#39;primary&#39;,</span><br><span class="line">        &#39;default-xfjbtn&#39;: this.type &#x3D;&#x3D;&#x3D; &#39;default&#39;,</span><br><span class="line">      &#125;]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">.xfjbtn &#123;</span><br><span class="line">  padding: 3px 5px;</span><br><span class="line">  border-radius: 3px;</span><br><span class="line">&#125;</span><br><span class="line">.default-xfjbtn &#123;</span><br><span class="line">  background-color: #fff;</span><br><span class="line">  color: black;</span><br><span class="line">&#125;</span><br><span class="line">.primary-xfjbtn &#123;</span><br><span class="line">  background-color: rgb(164, 164, 182);</span><br><span class="line">  color: white;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>组件注册到 Vue 全局的入口 <code>index.js</code></li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Button <span class="keyword">from</span> <span class="string">&#x27;./src/Button.vue&#x27;</span>;</span><br><span class="line"></span><br><span class="line">Button.install = <span class="function"><span class="keyword">function</span>(<span class="params">Vue</span>) </span>&#123;</span><br><span class="line">  Vue.component(<span class="string">&#x27;XfjButton&#x27;</span>, Button);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Button;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><p>组件的独立运行和展示</p>
<p> stories 文件夹下可存放组件的使用 demo。story 的格式如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> XfjButton <span class="keyword">from</span> <span class="string">&#x27;..&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  title: <span class="string">&#x27;Xfj/XfjButton&#x27;</span>,</span><br><span class="line">  component: XfjButton</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> DefaultButton = <span class="function">() =&gt;</span> (&#123;</span><br><span class="line">  components: &#123;</span><br><span class="line">    XfjButton,</span><br><span class="line">  &#125;,</span><br><span class="line">  template: <span class="string">`&lt;xfj-button type=&quot;default&quot;&gt;default&lt;/xfj-button&gt;`</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
<li><p>组件的测试</p>
<p> 使用 <code>jest</code> 进行单元测试。单元测试有如下的优点：</p>
<ol>
<li>减少新功能引发的 bug</li>
<li>减少手动测试时间</li>
<li>改进设计</li>
<li>促进重构</li>
<li>提供描述组件行为的文档</li>
</ol>
<p> 详细资料可查阅官网(<a target="_blank" rel="noopener" href="https://v1.test-utils.vuejs.org/zh/installation/testing-single-file-components-with-jest.html)[https://v1.test-utils.vuejs.org/zh/installation/testing-single-file-components-with-jest.html]%EF%BC%8C%E4%B8%8B%E9%9D%A2%E4%BB%8B%E7%BB%8D%E5%A4%A7%E8%87%B4%E4%BD%BF%E7%94%A8%E8%BF%87%E7%A8%8B%EF%BC%9A">https://v1.test-utils.vuejs.org/zh/installation/testing-single-file-components-with-jest.html)[https://v1.test-utils.vuejs.org/zh/installation/testing-single-file-components-with-jest.html]，下面介绍大致使用过程：</a></p>
<ol>
<li>安装插件<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ yarn add jest @vue&#x2F;test-utils@1 vue-jest babel-jest -D -W</span><br><span class="line">$ yarn add babel-core@bridge -D -W</span><br></pre></td></tr></table></figure>
<code>vue-jest</code>: 对 vue 单文件编译后的内容进行测试</li>
</ol>
<p> <code>babel-jest</code>: 对 esm 等 es6 进行支持</p>
<p> <code>babel-core@bridge</code>: @vue/test-utils 依赖 babel6（本项目安装的是 babel7）, 所以需要桥接转换</p>
<ol start="2">
<li><p>配置 <code>scripts</code></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;test&quot;</span>: <span class="string">&quot;jest&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>配置 <code>jest.config.js</code> 和 <code>babel.config.js</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// jest.config.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123; </span><br><span class="line">  testMatch: [<span class="string">&quot;**/__tests__/**/*.[jt]s?(x)&quot;</span>],</span><br><span class="line">  moduleFileExtensions: [</span><br><span class="line">    <span class="string">&quot;js&quot;</span>,</span><br><span class="line">    <span class="string">&quot;json&quot;</span>,</span><br><span class="line">    <span class="comment">// 告诉 jest 处理 .vue 文件</span></span><br><span class="line">    <span class="string">&quot;vue&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  transform: &#123;</span><br><span class="line">    <span class="comment">// 使用 vue-jest 处理 *.vue 文件</span></span><br><span class="line">    <span class="string">&quot;.*\\.(vue)$&quot;</span>: <span class="string">&quot;vue-jest&quot;</span>,</span><br><span class="line">    <span class="comment">// 使用 babel-jest 处理 js</span></span><br><span class="line">    <span class="string">&quot;.*\\.(js)$&quot;</span>: <span class="string">&quot;babel-jest&quot;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  testEnvironment: <span class="string">&quot;jsdom&quot;</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// babel.config.js </span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  presets: [<span class="string">&#x27;@babel/preset-env&#x27;</span>],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>常用的 jest API</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 全局函数</span></span><br><span class="line">describe(name, fn)  把相关测试组合在一起</span><br><span class="line">test(name, fn)      测试方法</span><br><span class="line">expect(value)       断言</span><br><span class="line"></span><br><span class="line"><span class="comment">// 匹配器</span></span><br><span class="line">toBe(value)             判断值是否相等</span><br><span class="line">toEqual(value)          判断对象是否相等</span><br><span class="line">toContain(value)        判断对象或者数组中是否包含 value</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="comment">// 快照</span></span><br><span class="line">toMatchSnapshot()</span><br></pre></td></tr></table></figure></li>
<li><p>常用的 @vue/test-utils 提供的 API</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建一个包含被挂载和渲染的 Vue 组件的 Wrapper</span></span><br><span class="line">mount()   </span><br><span class="line"><span class="comment">// wrapper </span></span><br><span class="line">Wrapper = &#123;</span><br><span class="line">  vm,</span><br><span class="line">  props(),</span><br><span class="line">  html(),</span><br><span class="line">  find(),</span><br><span class="line">  trigger(),</span><br><span class="line">  ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>示例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> XfjButton <span class="keyword">from</span> <span class="string">&#x27;../&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; mount &#125; <span class="keyword">from</span> <span class="string">&#x27;@vue/test-utils&#x27;</span>;</span><br><span class="line"></span><br><span class="line">describe(<span class="string">&#x27;Button 组件测试&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  test(<span class="string">&#x27;default button&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 在内存中的挂载</span></span><br><span class="line">    <span class="keyword">const</span> wrapper = mount(XfjButton, &#123;</span><br><span class="line">      propsData: &#123;</span><br><span class="line">        type: <span class="string">&#x27;default&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    expect(wrapper.html()).toContain(<span class="string">&#x27;default-xfjbtn&#x27;</span>);</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  test(<span class="string">&#x27;primary button&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> wrapper = mount(XfjButton, &#123;</span><br><span class="line">      propsData: &#123;</span><br><span class="line">        type: <span class="string">&#x27;primary&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// yarn test -u  会先删除已存在的快照，重新生成</span></span><br><span class="line">    expect(wrapper.vm.$el).toMatchSnapshot();</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>组件开发模板</p>
<p> 由于每个组件内部的结构是相同的，所以我们可以基于 plop 开发组件模板。具体实现如下：</p>
<ol>
<li>安装</li>
</ol>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ yarn add plop -D -W</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>根据模板生成配置文件的具体配置信息如下<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// plopfile.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="keyword">function</span> (<span class="params">plop</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// create your generators here</span></span><br><span class="line">  plop.setGenerator(<span class="string">&#x27;component&#x27;</span>, &#123;</span><br><span class="line">    description: <span class="string">&#x27;generator a custom component&#x27;</span>,</span><br><span class="line">    <span class="comment">// array of inquirer prompts</span></span><br><span class="line">    prompts: [</span><br><span class="line">      &#123;</span><br><span class="line">        type: <span class="string">&#x27;input&#x27;</span>,</span><br><span class="line">        name: <span class="string">&#x27;name&#x27;</span>,</span><br><span class="line">        message: <span class="string">&#x27;custom component name:&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    ], </span><br><span class="line">    <span class="comment">// array of actions</span></span><br><span class="line">    actions: [</span><br><span class="line">      &#123;</span><br><span class="line">        type: <span class="string">&#x27;add&#x27;</span>,</span><br><span class="line">        path: <span class="string">&#x27;src/&#123;&#123;name&#125;&#125;.js&#x27;</span>,</span><br><span class="line">        templateFile: <span class="string">&#x27;plop-templates/controller.hbs&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    ]  </span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
对于 plop 的模板，可参考仓库中的代码。具体不复述。</li>
</ol>
</li>
</ol>
<ol start="6">
<li><p>组件库打包</p>
<p> 使用 <code>Rollup</code> 进行打包，支持 tree-sharking，且打包后的体积小。打包过程中需要安装如下的第三方插件：</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ yarn add rollup rollup-plugin-terser rollup-plugin-vue@5.1.9 vue-template-compiler @rollup&#x2F;plugin-json @rollup&#x2F;plugin-node-resolve rollup-plugin-postcss -D -W</span><br></pre></td></tr></table></figure>

<p> <code>rollup-plugin-terser</code>: 代码压缩</p>
<p> <code>rollup-plugin-vue@5.1.9</code>: 版本5.1.9 是针对 vue2的。功能是将单文件组件编译为 js，内部依赖于 <code>vue-template-compiler</code></p>
<p> <code>@rollup/plugin-json</code>: 将 json 文件作为模块导入</p>
<p> <code>@rollup/plugin-node-resolve</code>: 将依赖的第三方插件也进行打包</p>
<p> 对于每个包的打包配置 <code>rollup.config.js</code>，具体配置如下：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; terser &#125; <span class="keyword">from</span> <span class="string">&#x27;rollup-plugin-terser&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> vue <span class="keyword">from</span> <span class="string">&#x27;rollup-plugin-vue&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123; </span><br><span class="line">  input: <span class="string">&#x27;index.js&#x27;</span>,</span><br><span class="line">  output: &#123;</span><br><span class="line">    file: <span class="string">&#x27;dist/bundle.js&#x27;</span>,</span><br><span class="line">    format: <span class="string">&#x27;cjs&#x27;</span></span><br><span class="line">  &#125;, </span><br><span class="line">  plugins: [ </span><br><span class="line">    vue(&#123; </span><br><span class="line">      <span class="comment">// 将单文件中的 css 解析后添加到元素的 style 标签上</span></span><br><span class="line">      css: <span class="literal">true</span>, </span><br><span class="line">      <span class="comment">// 将单文件编译为 js</span></span><br><span class="line">      compileTemplate: <span class="literal">true</span></span><br><span class="line">    &#125;),</span><br><span class="line">    terser(),</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> 由于仓库下存在多个包，为了统一管理打包流程，可在根目录下创建打包的配置文件。具体配置如下：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> fs <span class="keyword">from</span> <span class="string">&#x27;fs&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> path <span class="keyword">from</span> <span class="string">&#x27;path&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> json <span class="keyword">from</span> <span class="string">&#x27;@rollup/plugin-json&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; nodeResolve &#125; <span class="keyword">from</span> <span class="string">&#x27;@rollup/plugin-node-resolve&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; terser &#125; <span class="keyword">from</span> <span class="string">&#x27;rollup-plugin-terser&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> vue <span class="keyword">from</span> <span class="string">&#x27;rollup-plugin-vue&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> css <span class="keyword">from</span> <span class="string">&#x27;rollup-plugin-postcss&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> plugins = [</span><br><span class="line">  json(),</span><br><span class="line">  vue(&#123; </span><br><span class="line">    <span class="comment">// 将单文件中的 css 解析后添加到元素的 style 标签上</span></span><br><span class="line">    css: <span class="literal">true</span>, </span><br><span class="line">    <span class="comment">// 将单文件编译为 js</span></span><br><span class="line">    compileTemplate: <span class="literal">true</span></span><br><span class="line">  &#125;),</span><br><span class="line">  css(&#123; </span><br><span class="line">    extract: <span class="literal">true</span></span><br><span class="line">  &#125;),</span><br><span class="line">  nodeResolve(),</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">process.env.NODE_ENV === <span class="string">&#x27;production&#x27;</span> &amp;&amp; plugins.push(terser());</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> pakagesDir = path.resolve(__dirname, <span class="string">&#x27;packages&#x27;</span>);</span><br><span class="line"><span class="built_in">module</span>.exports = fs.readdirSync(pakagesDir)</span><br><span class="line">  .filter(<span class="function"><span class="params">item</span> =&gt;</span> fs.statSync(path.resolve(pakagesDir, item)).isDirectory())</span><br><span class="line">  .map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> pkg = <span class="built_in">require</span>(path.resolve(pakagesDir, item, <span class="string">&#x27;package.json&#x27;</span>));</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      input: path.resolve(pakagesDir, item, <span class="string">&#x27;index.js&#x27;</span>),</span><br><span class="line">      output: [</span><br><span class="line">        &#123;</span><br><span class="line">          file: path.resolve(pakagesDir, item, pkg.main),</span><br><span class="line">          format: <span class="string">&#x27;cjs&#x27;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          file: path.resolve(pakagesDir, item, pkg.module),</span><br><span class="line">          format: <span class="string">&#x27;esm&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">      ],</span><br><span class="line">      plugins,</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>

<p> 每次打包之前，需要先清楚之前打包的文件。具体操作如下：</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ yarn lerna clean &#x2F;&#x2F; 清楚各包下的 node_modules</span><br><span class="line">$ npm i rimraf &#x2F;&#x2F; rimraf 清楚指定的目录</span><br></pre></td></tr></table></figure></li>
<li><p>组件库发布</p>
<p> <code>lerna</code> 优化使用 git 和 npm 管理多包仓库的工作流工具。其特点如下：</p>
<ol>
<li>管理多包的 js 项目</li>
<li>一键提交代码到 github 或 npm</li>
</ol>
<p> 安装使用如下：</p>
 <figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ yarn add lerna -D -W</span><br><span class="line"><span class="comment">// 初始化（创建 .git、lerna.json、并将 lerna 添加到开发依赖中）</span></span><br><span class="line">$ lerna init</span><br><span class="line"><span class="comment">// 提交到 github 和 发布到 npm</span></span><br><span class="line">$ lerna public</span><br></pre></td></tr></table></figure>
<p> 提交到 npm 之前，需要确认当前 npm 的 registry 为 <code>https://registry.npmjs.org/</code></p>
</li>
</ol>
<h3 id="4-收尾"><a href="#4-收尾" class="headerlink" title="4. 收尾"></a>4. 收尾</h3><p>在开发中，为防止新功能引发的 bug，我们添加了 <code>jest</code> 测试。在团队开发中为了避免不规范的开发提交代码行为，可借助 <code>husky</code> 对代码提交做些校验。校验主要分两部分：<code>提交到 git 前需要测试成功</code>以及<code>提交信息的规范化</code>。</p>
<p>首先进行 <code>husky</code> 的安装，具体可参考<a href></a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ yarn add husky@4.3.8 -D -W</span><br></pre></td></tr></table></figure>

<h4 id="1-测试"><a href="#1-测试" class="headerlink" title="1. 测试"></a>1. 测试</h4><p>通过 <code>pre-commit</code> 执行 git 提交前的事件，具体使用如下：</p>
<ol>
<li><p>安装插件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ yarn add pre-commit -D -W</span><br></pre></td></tr></table></figure></li>
<li><p>配置执行的命令</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;husky&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;hooks&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">&quot;pre-commit&quot;</span>: <span class="string">&quot;npm run test&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="2-提交信息"><a href="#2-提交信息" class="headerlink" title="2. 提交信息"></a>2. 提交信息</h4><ol>
<li><p>插件安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ yarn add @commitlint&#x2F;cli @commitlint&#x2F;config-conventional -D -W</span><br></pre></td></tr></table></figure></li>
<li><p>配置文件 <code>commitlint.config.js</code></p>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="keyword">extends</span>: [<span class="string">&#x27;@commitlint/config-conventional&#x27;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>配置的命令</li>
</ol>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;husky&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;hooks&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">&quot;commit-msg&quot;</span>: <span class="string">&quot;commitlint -E HUSKY_GIT_PARAMS&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://xiaofujun.gitee.io/2022/03/21/%E6%A8%A1%E6%8B%9F%E7%BB%84%E4%BB%B6%E5%BA%93%E5%BC%80%E5%8F%91%E6%B5%81%E7%A8%8B/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue/" rel="tag">vue</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2022/03/24/%E9%9D%99%E6%80%81%E7%AB%99%E7%82%B9SSG/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            静态站点SSG
          
        </div>
      </a>
    
    
      <a href="/2022/03/05/%E6%8E%A2React%E4%B9%8B%E6%97%85%E4%B8%80/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">探React之旅一</div>
      </a>
    
  </nav>

  
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> 小付君
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="君书"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->

<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>